    

export enum ENV{
    test = "测试环境",
    master = "正式环境"
}
import { web3ConfigInit , getWeb3Config} from "./config";
import {init as loginInit} from './loginTwo';
import web3 from 'web3/dist/web3.min.js'
import {SmartContract,GoodsType} from "./smartContract";
import UtilTool from "../../tools/UtilTool";
class Web3 {
    private static test: string;
    private static instance: Web3 = null;
    private web3Config;
    account:string = null;
    loginSuccess:Boolean = false;
    smartContract: SmartContract;
    static loginSuccess: any;
    static smartContract: any;
    private constructor(env:ENV) {
        Web3.test = '未初始化'
        web3ConfigInit(env)
        this.web3Config =  getWeb3Config();
       
        // this.r
    }
    public static init(env:ENV){
        UtilTool.printLog(this)
        this.instance = this.instance || new Web3(env);
        this.test = '已初始化'
        return this.instance;
    }

    /**
     * 
     * @param param0 signRandom:获取后端签名随机数的方法
     * @param param1 login:签名之后发给后端验签的方法，成功即为登录成功
     * @returns 
     */
    public async login({chainIndex,signRandom,loginWallet}){
        try {
            if (!Web3.instance) {
                return '未初始化web3,请调用对应方法init'
            }
            UtilTool.printLog(this.web3Config.rpc,'this.web3Config.rpc');
            
            let login = loginInit(this.web3Config.rpc);
            //引导用户切换网络 并获取钱包地址
            let userAddress = await login.connectNetwork(chainIndex); 
            UtilTool.printLog(userAddress,'userAddress');
            
            if (userAddress) {
                //切换网络成功返回用户地址后，再调用登录方法。
                let nonce = await signRandom(userAddress); //获取签名
                const signature =  await login.web3.eth.personal.sign(new web3().utils.sha3(nonce.randomCode), userAddress);
                let loginRes:any = await loginWallet({account:userAddress,sign:signature});
                
                
                UtilTool.printLog(this.web3Config,'this.web3Config');
                
                //登录成功 初始化合约
                Web3.smartContract = new SmartContract(userAddress,login.web3,{recABI:this.web3Config.ABI.rec20ABI,
                                                                    recURL:this.web3Config.ABI.rec20Url,
                                                                    gameABI:this.web3Config.ABI.gameABI,
                                                                    gameURL:this.web3Config.ABI.gameUrl});
                if (loginRes.ret == 0) {
                    Web3.loginSuccess = true;
                    return loginRes;
                }else{
                    return null;
                }
            }
            
        } catch (error) {
            UtilTool.printLog(error,'登录失败');
            
        }
    }
    //授权 
    public static async authorize(goodsType:GoodsType,amount:number, goodsPrice:number){
        if (!Web3.loginSuccess) {
            alert('未登录拒绝调用授权')
        }
        return await Web3.smartContract.authorize(goodsType,amount,goodsPrice)
        
    }
    //支付
    public static async play(orderId){
        if (!Web3.loginSuccess) {
            alert('未登录拒绝调用支付')
        }
        UtilTool.printLog( Web3.smartContract,' Web3.smartContract');
        
        return await Web3.smartContract.play(orderId)
    }

    public static  async contractPlayRecord(finishTime,sign) {
        if (!Web3.loginSuccess) {
            alert('未登录拒绝调用上传')
        }
        return await Web3.smartContract.contractPlayRecord(finishTime,sign)
    }
    // login
    // toString() {
    //   return '(' + this.x + ', ' + this.y + ')';
    // }
};
export const init = (env:ENV) =>{
   return  Web3.init(env)
};

//上传记录
export const contractPlayRecord =  async (finishTime,sign) =>{
    return  await Web3.contractPlayRecord(finishTime,sign)
};
//支付授权
export const authorize =  async (goodsType:GoodsType, amount:number, goodsPrice:number) =>{
    UtilTool.printLog('调用了支付授权，当前商品id是：' + goodsType);
    
    return  await Web3.authorize(goodsType,amount,goodsPrice)
};
//支付
export const play = async(orderId) =>{
    return await Web3.play(orderId)
};
// export const login = () =>{
//     web3.login()
// }
// export  web3.init2;